﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using EC_Agent.Models;
using EC_Agent.AgentClass;

namespace EC_Agent
{
    public partial class KhachHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillKhachHangGrid();
            }
        }

        private void FillKhachHangGrid()
        {
            gvAGENT.DataSource = new KhachHangModels().ListKhachHang();
            gvAGENT.DataBind();
        }

        protected void gvAGENT_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAGENT.PageIndex = e.NewPageIndex;
            FillKhachHangGrid();
        }

        protected void gvAGENT_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvAGENT.EditIndex = -1;
            FillKhachHangGrid();
        }

        protected void gvAGENT_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Detail"))
            {
                LinkButton lnkBtn = (LinkButton)e.CommandSource;    // the button
                GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent;  // the row
                GridView myGrid = (GridView)sender; // the gridview
                string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString(); // value of the
                Response.Redirect("adDetailKhachHang.aspx?ID=" + ID);
            }
        }

        protected void gvAGENT_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int16 ID = Convert.ToInt16(gvAGENT.DataKeys[e.RowIndex].Values[0].ToString());
            KhachHangInfo eInfo = new KhachHangInfo();
            eInfo.MaKhachHang = ID;
            new KhachHangModels().DeleteKhachHang(eInfo);
            FillKhachHangGrid();
        }

        protected void gvAGENT_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvAGENT.EditIndex = e.NewEditIndex;
            FillKhachHangGrid();
        }

        protected void gvAGENT_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            KhachHangInfo eInfo = new KhachHangInfo();
            eInfo.TinhTrang = ((CheckBox)gvAGENT.Rows[e.RowIndex].FindControl("chkTinhTrang")).Checked;
            new KhachHangModels().UpdateKhachHang(eInfo);
            gvAGENT.EditIndex = -1;
            FillKhachHangGrid();
        }
    }
}